<%@ page import="entity.User" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="java.util.Map" %><%--
  Created by IntelliJ IDEA.
  User: User
  Date: 2024/9/4
  Time: 15:50
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>EL表达式</title>
</head>
<body>
    <%
        //模拟数据
        pageContext.setAttribute("msg","hello pageContext");
        request.setAttribute("msg","hello request");
        session.setAttribute("msg","hello session");
        application.setAttribute("msg","hello application");
        User user=new User(1,"admin","123456","1122222","江苏-南京");
        User user2=new User(2,"alice","2222","1122222","江苏-南京");
        User user3=new User(3,"coco","3333","1122222","江苏-南京");
        User user4=new User(4,"lucy","123444456","1122222","江苏-南京");
        request.setAttribute("user",user);
        List<User> users=new ArrayList<User>();
        users.add(user);
        users.add(user2);
        users.add(user3);
        users.add(user4);
        request.setAttribute("users",users);
        List<Integer> nums=new ArrayList<Integer>();
        request.setAttribute("nums",nums);
        Map<String,User> map=new HashMap<String,User>();
        map.put("u1",user);
        map.put("u2",user2);
        map.put("u3",user3);
        map.put("u4",user4);
        request.setAttribute("map",map);
    %>
    <h2>访问作用域</h2>

   <%--
    ${作用域对象.key} 用于获取指定的作用域中指定key的数据
    可以直接使用${key} 来获取作用域的数据
    此时会根据作用域的从小到大依次查看所有的作用域中的数据
    pageContext  request  session  application
    如果找到了对应的数据,则不再查找,直接返回对应的数据
    如果没有找到对应的数据
    则依次通过request、session、application进行查找,直到找到对应的数据为止
    如果所有的作用域中都没有找到
    则返回一个空数据,在页面中相当于什么都没有显示
    --%>

    PageContext:${pageScope.msg}<br>
    request:${requestScope.msg}<br>
    session:${sessionScope.msg}<br>
    application:${applicationScope.msg}<br>
    msg:${msg}
    <br>
    <%--在EL表达式中会遇到类似于java中get/set方法,直接省略get关键字,首字母变成小写，${对象.属性名}--%>
    user:${user.id}--${user.username}--${user.password}--${user.phone}--${user.address}
    <br>
    <h2>获取应用名</h2>
    <%
        HttpServletRequest req= (HttpServletRequest) pageContext.getRequest();
        String path=req.getContextPath();
        out.print(path);
    %>
    <br>
    EL表达式获取应用名:${pageContext.request.contextPath}<br>

    <h2>获取请求参数</h2>
    <%
        String id=request.getParameter("id");
        out.print(id);
        String username=request.getParameter("username");
        out.print(username);
    %>
    <br>
    ${param.id}--${param.username}
    <br>
    <h2>获取List集合数据</h2>
    <%--
        获取List集合的数据，本质上就是get方法
        users.get(index)-users[index]
    --%>
    ${users[0].id}--${users[1].username}--${users[2].password}

    <h2>获取Map集合</h2>
    <%--
        两种语法:
            map[key]
            map.key:该方式的key不能以数字开头，即必须符合编码规范
    --%>
    ${map["u1"].id}--${map["u1"].username}--${map["u1"].password}
    ${map.u2.id}--${map.u2.username}--${map.u2.password}

    <!--EL表达式的运算类似于js-->
    3+2=${3+2} <br>
    3-2=${3-2} <br>
    3*2=${3*2} <br>
    3/2=${3/2} <br>
    3%2=${3%2} <br>
    ${1=="1"}<br>
    ${"2" eq "1"} <br>
    ${"2" eq "2"} <br>
    user是否为空:${empty user} <br>
    ${empty users}<br>
    ${empty nums}<br>
    三目:${2==10?"checked":"no-checked"}<br>
    三目:${7==7?"yes":"no"}<br>
    且:${true and false}<br>
    或:${true or false}<br>
</body>
</html>
